<template>
  <div id="app">
    <div id="chart-container" ref="chartContainer" style="height: 100%; width: 100%; position: absolute; top: 0; left: 0;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'EchartsDemo',
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart);
  },
  methods: {
    initChart() {
      const chartContainer = this.$refs.chartContainer;
      this.myChart = echarts.init(chartContainer, null, {
        renderer: 'canvas',
        useDirtyRect: false
      });
      const option = {
        title: {
          text: '标准数据统计',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          confine: true,          // 强制提示框限制在图表区域内
          position: 'right',      // 优先右侧显示（根据实际情况调整）
        },
        legend: {
          orient: 'horizontal',
          bottom: '10',
          left: 'center'
        },
        series: [
          {
            name: '标准类型',
            type: 'pie',
            radius: '50%',
            data: [
              { value: 135, name: '国家标准' }, // National Standard
              { value: 865, name: '行业标准' }, // Industry Standard
              { value: 400, name: '企业标准' }   // Enterprise Standard
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ],
        toolbox: {
          top: '30vh',
          feature: {
            saveAsImage: {
              show: true,
              title: '下载图片'
            },
            restore: {
              show: true,
              title: '刷新'
            },
            dataView: {   // 添加此行以启用数据视图功能
              show: true,
              readOnly: false, // 设置为 false 允许编辑数据，true 则只读
              title: '数据视图',
              lang: ['数据视图', '关闭', '刷新'], // 自定义工具提示文本
              optionToContent: function(opt) {
                // 自定义数据视图内容的格式化函数
                var axisData = opt.series[0].data;
                var seriesData = '';
                for (var i = 0; i < axisData.length; i++) {
                  seriesData += axisData[i].name + ': ' + axisData[i].value + '\n';
                }
                return seriesData;
              },
              contentToOption: function(option, content) {
                // 如果需要自定义从文本到选项的转换逻辑，可以在这里实现
                return option;
              }
            }
          }
        }
      };

      if (option && typeof option === 'object') {
        this.myChart.setOption(option);
      }
    },
    resizeChart() {
      this.myChart.resize();
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
#chart-container {
  position: relative;
  height: 80vh;
  overflow: hidden;
}
</style>
